#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream>
#include "Log.hpp"
#include "Common.hpp"
using namespace LogModule;
const std::string gsep = ": ";
const std::string gfilename = "dict.txt";
const std::string gpath = "./";
class Dictionary
{
public:
    Dictionary(const std::string path = gpath, std::string filename = gfilename)
        : _path(path), _filename(filename)
    {
    }
    bool LoadDictionary()
    {
        std::string file = _path + _filename;
        std::ifstream in(file.c_str());
        if (in.is_open() == 0)
        {
            LOG(LogLevel::ERROR) << "open file  " << file.c_str() << " fail";
            return false;
        }
        std::string line;
        while (std::getline(in, line))
        {
            std::string key;
            std::string value;
            if (SplitString(line, &key, &value, gsep))
            {
                _dictionary[key] = value;
            }
        }
        Print();
        LOG(LogLevel::DEBUG) << "Print over";
        in.close();
        return true;
    }
    std::string Translate(const std::string &word)
    {
        auto value = _dictionary.find(word);
        if (value == _dictionary.end())
        {
            return "Bool False";
        }
        return value->second;
    }
    void Print()
    {
        for (auto &item : _dictionary)
        {
            std::cout << item.first << " :" << item.second << std::endl;
        }
    }
    ~Dictionary()
    {
    }

private:
    std::unordered_map<std::string, std::string> _dictionary;
    std::string _path;
    std::string _filename;
};
